import { GangFightsMagicStoneWindow } from "./GangFightsMagicStoneWindow";
import { GangFightsMeritWindow } from "./GangFightsMeritWindow";
import { GangFightsCreateTeamWindow } from "./GangFightsCreateTeamWindow";
import { GangFightsChangeCaptainWindow } from "./GangFightsChangeCaptainWindow";
import { GangFightsKickOutWindow } from "./GangFightsKickOutWindow";
import { GangFightsInviteToTeamWindow } from "./GangFightsInviteToTeamWindow";
import { Alert } from "../../../../com/mui/controls/Alert";
import { Language } from "../../../../extend/language/Language";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { SGangFightsTeamInfo } from "../../../../Message/Public/SGangFightsTeamInfo";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { GameDefConfig } from "../../resource/GameDefConfig";
import { ObjTeamCreate } from "../common/ObjTeamCreate";
import { SEntityId } from "../../../../Message/Public/SEntityId";
import { SLookupPlayerReply } from "../../../../Message/Game/SLookupPlayerReply";
import { EGangFightsTeamPos } from "../../../../Message/Public/EGangFightsTeamPos";
import { Cache } from "../../cache/Cache";
import { SGangFightsTeamMember } from "../../../../Message/Public/SGangFightsTeamMember";
import { ObjCreate } from "../common/ObjCreate";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { GangFightsMyTeamPlayerCell } from "./cellRenderer/GangFightsMyTeamPlayerCell";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { ResFileConst } from "../../resource/ResFileConst";
import { GButton } from "../../../../com/mui/controls/GButton";
type int = number;
//class GangFightsMyTeamInfoPanel
    
    export  class GangFightsMyTeamInfoPanel /* flash.display.Sprite */
    {
        constructor()
        {
            
            this.init();
            this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddToStage);
            return;
        }

        /* internal  */onLink1(arg1: Object /* flash.events.TextEvent */): void
        {
            GangFightsMagicStoneWindow.instance.show();
            return;
        }

        /* internal  */onLink2(arg1: Object /* flash.events.TextEvent */): void
        {
            GangFightsMeritWindow.instance.show();
            return;
        }

        /* internal  */onClickChangeMsg(arg1: Object /* flash.events.MouseEvent */): void
        {
            GangFightsCreateTeamWindow.instance.show();
            GangFightsCreateTeamWindow.instance.isModifyMode = true;
            if (this._teamInfo) 
            {
                GangFightsCreateTeamWindow.instance.updateTeamMsg(this._teamInfo.name, this._teamInfo.faceId);
            }
            return;
        }

        /* internal  */onClickChangeCaptain(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._teamInfo) 
            {
                GangFightsChangeCaptainWindow.instance.show();
                GangFightsChangeCaptainWindow.instance.updateTeamInfo(this._teamInfo);
            }
            return;
        }

        /* internal  */onClickKickOut(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._teamInfo) 
            {
                GangFightsKickOutWindow.instance.show();
                GangFightsKickOutWindow.instance.updateTeamInfo(this._teamInfo);
            }
            return;
        }

        /* internal  */onClickInviteTeam(arg1: Object /* flash.events.MouseEvent */): void
        {
            GangFightsInviteToTeamWindow.instance.show();
            return;
        }

        /* internal  */onClickDisbandTeam(arg1: Object /* flash.events.MouseEvent */): void
        {
            Alert.show(Language.getString(44520), null, Alert.OK | Alert.CANCEL, null, this.onCloseAlert);
            return;
        }

        /* internal  */onCloseAlert(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GangFightsDisbandTeam));
            }
            return;
        }

        /* internal  */onClickLeaveTeam(arg1: Object /* flash.events.MouseEvent */): void
        {
            Alert.show(Language.getString(44553), null, Alert.OK | Alert.CANCEL, null, this.onCloseLeaveAlert);
            return;
        }

        /* internal  */onCloseLeaveAlert(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GangFightsLeaveTeam));
            }
            return;
        }

        public updateTeamInfo(arg1: SGangFightsTeamInfo): void
        {
            this._teamInfo = arg1;
            this.updateView();
            this.updateButtonVisible();
            if (GangFightsChangeCaptainWindow.hasInstance()) 
            {
                GangFightsChangeCaptainWindow.instance.updateTeamInfo(this._teamInfo);
            }
            if (GangFightsKickOutWindow.hasInstance()) 
            {
                GangFightsKickOutWindow.instance.updateTeamInfo(this._teamInfo);
            }
            return;
        }

        public updateView(): void
        {
            if (this._teamInfo && this._isResCompl) 
            {
                this._bmpFace.bitmapData = GlobalClass.getBitmapData(GameDefConfig.instance.getGangFightsFaceName(this._teamInfo.faceId));
                this._tfTeamName.text = this._teamInfo.name;
                ObjTeamCreate.setTextFieldKeyValue(this._tfScore, Language.getString(44512), this._teamInfo.mark.toString());
                ObjTeamCreate.setTextFieldKeyValue(this._tfRank, Language.getString(44513), this._teamInfo.rank.toString());
                this._teamPlayer1.updateMember(null);
                this._teamPlayer2.updateMember(null);
                this._teamPlayer3.updateMember(null);
                this._teamInfo.members.sort(this.isMemberCaptain);
                if (this._teamInfo.members.length > 0) 
                {
                    this._teamPlayer2.updateMember(this._teamInfo.members[0]);
                }
                if (this._teamInfo.members.length > 1) 
                {
                    this._teamPlayer1.updateMember(this._teamInfo.members[1]);
                }
                if (this._teamInfo.members.length > 2) 
                {
                    this._teamPlayer3.updateMember(this._teamInfo.members[2]);
                }
            }
            return;
        }

        public setPlayerNotOnline(arg1: SEntityId): void
        {
            this._teamPlayer1.updatePlayerNotOnline(arg1);
            this._teamPlayer2.updatePlayerNotOnline(arg1);
            this._teamPlayer3.updatePlayerNotOnline(arg1);
            return;
        }

        public updateTeamPlayerInfo(arg1: SLookupPlayerReply): void
        {
            this._teamPlayer1.updatePlayerInfo(arg1);
            this._teamPlayer2.updatePlayerInfo(arg1);
            this._teamPlayer3.updatePlayerInfo(arg1);
            return;
        }

        /* internal  */updateButtonVisible(): void
        {
            var loc1=this.isCaptain();
            this._btnChangeMsg.visible = loc1;
            this._btnChangeCaptain.visible = loc1;
            this._btnKickOut.visible = loc1;
            this._btnInviteTeam.visible = loc1;
            this._btnDisbandTeam.visible = loc1;
            this._btnLeaveTeam.visible = !loc1;
            if (!loc1) 
            {
                if (GangFightsInviteToTeamWindow.hasInstance()) 
                {
                    GangFightsInviteToTeamWindow.instance.hide();
                }
                if (GangFightsChangeCaptainWindow.hasInstance()) 
                {
                    GangFightsChangeCaptainWindow.instance.hide();
                }
                if (GangFightsKickOutWindow.hasInstance()) 
                {
                    GangFightsKickOutWindow.instance.hide();
                }
                if (GangFightsCreateTeamWindow.hasInstance()) 
                {
                    GangFightsCreateTeamWindow.instance.hide();
                }
            }
            return;
        }

        /* internal  */isCaptain(): Boolean
        {
            var loc1=null;
            if (!this._teamInfo) 
            {
                return false;
            }
            var loc2=0;
            var loc3=this._teamInfo.members;
            for(loc1 of loc3) 
            {
                if (!(loc1.pos.value() == EGangFightsTeamPos._EGangFightsTeamLeader && loc1.name == Cache.instance.role.entityInfo.name)) 
                {
                    continue;
                }
                return true;
            }
            return false;
        }

        /* internal  */isMemberCaptain(arg1: SGangFightsTeamMember, arg2: SGangFightsTeamMember): int
        {
            if (arg1.pos.value() >= arg2.pos.value()) 
            {
                return -1;
            }
            return 1;
        }

        /* internal  */onAddToStage(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GangFightsGetTeamInfo, Cache.instance.gangFights.myInfo.teamId));
            return;
        }

        /* internal  */init(): void
        {
            ObjCreate.createBg(0, 0, 802, 483, this);
            ObjCreate.createBg(50, 5, 64, 64, this);
            ObjCreate.createFlowerBg(0, 0, 802, 78, this, true);
            this._bmpFace = new Object /* flash.display.Bitmap */();
            ObjCreate.setObjAttri(this._bmpFace, 52, 7, -1, -1, this);
            var loc1=GlobalStyle.textFormat6;
            loc1.size = 14;
            loc1.bold = true;
            this._tfTeamName = ObjCreate.createTextField("", 128, 26, 200, 20, this, loc1);
            this._tfScore = ObjCreate.createTextField(Language.getString(44512), 260, 10, 100, 20, this);
            this._tfRank = ObjCreate.createTextField(Language.getString(44513), 260, 50, 100, 20, this);
            var loc2=GlobalStyle.textFormat1;
            loc2.leading = 7;
            this._desc1 = ObjCreate.createTextField("", 375, 10, 360, 40, this, loc2);
            this._desc1.multiline = true;
            this._desc1.wordWrap = true;
            this._desc1.htmlText = Language.getString(44558);
            this._desc2 = ObjCreate.createTextField("", 375, 50, 400, 40, this);
            this._desc2.htmlText = Language.getString(44559);
            this._teamPlayer1 = new GangFightsMyTeamPlayerCell();
            this._teamPlayer1.x = 64;
            this._teamPlayer1.y = 115;
            this.addChild(this._teamPlayer1);
            this._teamPlayer2 = new GangFightsMyTeamPlayerCell();
            this._teamPlayer2.x = 309;
            this._teamPlayer2.y = 80;
            this.addChild(this._teamPlayer2);
            this._teamPlayer3 = new GangFightsMyTeamPlayerCell();
            this._teamPlayer3.x = 549;
            this._teamPlayer3.y = 115;
            this.addChild(this._teamPlayer3);
            this._btnChangeMsg = ObjCreate.createGButton(Language.getString(44514), 201, 451, 70, 26, this);
            this._btnChangeCaptain = ObjCreate.createGButton(Language.getString(44515), 284, 451, 70, 26, this);
            this._btnKickOut = ObjCreate.createGButton(Language.getString(44516), 367, 451, 70, 26, this);
            this._btnInviteTeam = ObjCreate.createGButton(Language.getString(44517), 450, 451, 70, 26, this);
            this._btnDisbandTeam = ObjCreate.createGButton(Language.getString(44518), 533, 451, 70, 26, this);
            this._btnLeaveTeam = ObjCreate.createGButton(Language.getString(44519), 367, 451, 70, 26, this);
            this.updateButtonVisible();
            this.addListeners();
            LoaderHelp.addResCallBack(ResFileConst.GangFightsFace, this.onResCompl);
            return;
        }

        /* internal  */onResCompl(): void
        {
            this._isResCompl = true;
            this.updateView();
            return;
        }

        /* internal  */addListeners(): void
        {
            this._desc1.addEventListener(flash.events.TextEvent.LINK, this.onLink1);
            this._desc2.addEventListener(flash.events.TextEvent.LINK, this.onLink2);
            this._btnChangeMsg.addEventListener(flash.events.MouseEvent.CLICK, this.onClickChangeMsg);
            this._btnChangeCaptain.addEventListener(flash.events.MouseEvent.CLICK, this.onClickChangeCaptain);
            this._btnKickOut.addEventListener(flash.events.MouseEvent.CLICK, this.onClickKickOut);
            this._btnInviteTeam.addEventListener(flash.events.MouseEvent.CLICK, this.onClickInviteTeam);
            this._btnDisbandTeam.addEventListener(flash.events.MouseEvent.CLICK, this.onClickDisbandTeam);
            this._btnLeaveTeam.addEventListener(flash.events.MouseEvent.CLICK, this.onClickLeaveTeam);
            return;
        }

        /* internal */ /* var */_bmpFace: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_tfTeamName: Object /* flash.text.TextField */;

        /* internal */ /* var */_tfScore: Object /* flash.text.TextField */;

        /* internal */ /* var */_tfRank: Object /* flash.text.TextField */;

        /* internal */ /* var */_desc1: Object /* flash.text.TextField */;

        /* internal */ /* var */_desc2: Object /* flash.text.TextField */;

        /* internal */ /* var */_teamPlayer1: GangFightsMyTeamPlayerCell;

        /* internal */ /* var */_teamPlayer2: GangFightsMyTeamPlayerCell;

        /* internal */ /* var */_teamPlayer3: GangFightsMyTeamPlayerCell;

        /* internal */ /* var */_btnChangeMsg: GButton;

        /* internal */ /* var */_btnChangeCaptain: GButton;

        /* internal */ /* var */_btnKickOut: GButton;

        /* internal */ /* var */_btnInviteTeam: GButton;

        /* internal */ /* var */_btnDisbandTeam: GButton;

        /* internal */ /* var */_btnLeaveTeam: GButton;

        /* internal */ /* var */_teamInfo: SGangFightsTeamInfo;

        /* internal */ /* var */_isResCompl: Boolean=false;
    }
